using Godot;
using System;

namespace Box;

public partial class ItemSelectControl : Control
{
	[Signal]
	public delegate void onConfirmEventHandler(string class_full_name);

	public ItemSelectTree ItemSelectTree;
	public Button ConfirmButton;
	public LineEdit ClassNameDisplay;
	public CodeEditControl CodeEditControl;
	string select_class_name = "";

	public override void _Ready()
	{
		ItemSelectTree = GetNode<ItemSelectTree>("PanelContainer/VBoxContainer/HSplitContainer/PanelContainer/MarginContainer/ItemSelectTree");
		ConfirmButton = GetNode<Button>("PanelContainer/VBoxContainer/HBoxContainer2/PanelContainer2/MarginContainer/ConfirmButton");
		ClassNameDisplay = GetNode<LineEdit>("PanelContainer/VBoxContainer/HBoxContainer2/PanelContainer/MarginContainer/LineEdit");
		CodeEditControl =  GetNode<CodeEditControl>("PanelContainer/VBoxContainer/HSplitContainer/PanelContainer2/MarginContainer/CodeEditControl");

		ItemSelectTree.onSelect += _onSelectClassFile;

		CodeEditControl.IsPreviewMode = true;

		ConfirmButton.ButtonDown += _onConfirm;
	}

	public void _onSelectClassFile(string class_name,string class_file_path) {
		select_class_name = class_name;
		ClassNameDisplay.Text = select_class_name;
		CodeEditControl.Open(class_file_path);
	}

	public void _onConfirm() {
		if(select_class_name != "") {
			EmitSignal(nameof(onConfirm),select_class_name);
			select_class_name = "";
		}
	}

	// Called every frame. 'delta' is the elapsed time since the previous frame.
	public override void _Process(double delta)
	{
	}
}
